﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace odinshrine
{
    class Humain : SpriteManager
    {

        public float anglee;
        public float angleF;
        public Vector2 OrigineT = Vector2.Zero;
        public Vector2 DirectionT = Vector2.Zero;
        public Vector2 OrigineA = Vector2.Zero;
        public float kneelingplus = 0f;
        private float upTime = 200;
        public int swal = 1;
        public float finalAngle = 0;
        public SpriteList ballesListe = new SpriteList(100);
        public SpriteList directionBalles = new SpriteList(100);
        public TimeSpan timer_tirs;
        public Random alea = new Random();

        public Vector2 lastPos = new Vector2(0, 0);

        public int Vie = 5;
        public int Vitesse = 3;
        public Arme arme = Armerie.Deagle;
        public int ammo;
        protected int shooted;
        protected float lastangle;
        private float dAngle;

        public float angle
        {
            get
            {
                return (angleF);
            }

            set
            {
                /* Gestion de l'angle des tirs */
                { OrigineA.X = (float)(Position.X + (texture.Width / (width) )); }
                { OrigineA.Y = (float)(Position.Y + (texture.Height / (height) * 3) - 5 + kneelingplus); }
                { OrigineT.X = (float)((OrigineA.X + 30 * (swal) * (Math.Cos(value)))); }
                { OrigineT.Y = (float)(OrigineA.Y - 30 * (Math.Sin(value))); }
                { DirectionT = (OrigineT - OrigineA) /10; }
                { this.angleF = value; }
            }
        }
        // Changement des angles de tir ;p
        public void AngleMe(GameTime gameTime)
        {
            if (anglee > finalAngle - Math.PI / 20)
            {
                anglee -= (float)Math.PI / 30;
            }

            else
            {
                if (anglee < finalAngle + Math.PI / 20)
                {
                    anglee += (float)Math.PI / 30;
                }
                else
                {
                    anglee = finalAngle;
                }
            }

            this.angle = anglee;

        }

        public void MouvementBalles(GameTime gameTime)
        {
            // Gestion Tir
            for (int i = 0; i < 100; i++)
            {
                // Mouvement des balles
                if ((float)Math.Sqrt(Math.Pow(ballesListe.tableau[i, 0] - Position.X, 2) + Math.Pow(ballesListe.tableau[i, 1]- Position.Y, 2)) < arme.portee)
                {
                    ballesListe.tableau[i, 0] += (int)(directionBalles.tableau[i, 0] * gameTime.ElapsedGameTime.Milliseconds);
                    ballesListe.tableau[i, 1] += (int)(directionBalles.tableau[i, 1] * gameTime.ElapsedGameTime.Milliseconds);
                }
                else
                {
                    ballesListe.tableau[i, 0] = -1;
                    ballesListe.tableau[i, 1] = -1;
                    directionBalles.tableau[i, 0] = 0;
                    directionBalles.tableau[i, 1] = 0;
                }
            }
        }
        public void Tir()
        {
            {
                    dAngle = ((float)(alea.Next((int)(arme.spread * 20000))) / (float)10000) - (arme.spread);
                    this.angle = lastangle + dAngle;
                    ballesListe.Add(OrigineT, ballesListe.tableau);
                    directionBalles.Add(DirectionT, ballesListe.tableau);
                    Son.PlayShoot(arme);
                    shooted--;
                    ammo--;
                    this.angle = lastangle;
                }
            
        }   
    }
}
